package com.haogu.controller;

import com.haogu.entity.Leads;
import com.haogu.entity.PLeads;
import com.haogu.entity.User;
import com.haogu.entity.VLeads;
import com.haogu.service.LeadsService;
import com.haogu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/leads")
public class LeadsController {
    @Resource
    private UserService userService;
    @Resource
    private LeadsService leadsService;
    /**
     * 点击线索池跳转页面
     */
    @RequestMapping("/toLeadsChi")
    public String toLeadsChi(){
        return "leads/list_leads_chi";
    }
    /**
     * 点击线索管理跳转页面
     */
    @RequestMapping("/toLeads")
    public String toLeads(){
        return "leads/leads";
    }
    /**
     * 添加
     *
     */
    @RequestMapping("toAddLeads")
    public String toAdd(Model model){
        List<User> users = userService.selectAllUser();
        model.addAttribute("users",users);
        return "leads/add_leads.html";
    }

    @RequestMapping("/addLeads")
    @ResponseBody
    public Map add(Leads leads, HttpSession session){
        User user = (User)session.getAttribute("user");
        leads.setCreatorUserId(user.getId());
        if(leads.getOwnerUserId()==0)
        {
            leads.setOwnerUserId(null);
        }
        boolean b = leadsService.insertLeads(leads);
        Map map = new HashMap<>();
        if(b){
            map.put("success",true);
        }else{
            map.put("success",false);
        }
        return  map;
    }

    /**
     * 展示
     * @return
     */
    @RequestMapping("/toListLeads")
    public String toList(){
        return "leads/list_leads";
    }
    @RequestMapping("/listLeads")
    @ResponseBody
    public Map searchLeads(PLeads p,Model model,HttpSession session){
        System.out.println(p);
        User user = (User)session.getAttribute("user");
        //判断负责人是否为空
        if(p.getFuzeren()!=null){
            if("me".equals(p.getFuzeren())){
                p.setUserid(user.getId());
            }else if("xiashu".equals(p.getFuzeren())){
                // 查询下属的ids
                List<Integer> integers = userService.selectOtherUserIdById(user.getId());
                p.setIds(integers);
            }
        }


        Map map = new HashMap();
        int rows1 = leadsService.rows();
        List<VLeads> vLeads = leadsService.searchLeads(p);
        map.put("totalRows",rows1);
        map.put("result",vLeads);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }
    @RequestMapping("/listLeadsChi")
    @ResponseBody
    public Map searchLeadsChi(PLeads p,Model model){
        Map map = new HashMap();
        int rows1 = leadsService.rows();
        List<VLeads> vLeads = leadsService.searchLeadsChi(p);
        map.put("totalRows",rows1);
        map.put("result",vLeads);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }
    /**
     *查看
     */
    @RequestMapping("/toShowLeads")
    public String show(){
        return "leads/show_leads";
    }

    @RequestMapping("/showLeads")
    public String showLeads(Integer id, ModelMap map){
        Leads leads = leadsService.selectLeadsById(id);
        User user = userService.findUserById(leads.getCreatorUserId());
        map.put("leads",leads);
        map.put("user",user);
        return "leads/show_leads";
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteLeads")
    @ResponseBody
    public Map deleteLeads(String ids){
        String[] split = ids.split(",");
        boolean flag = true;
        for(int i = 0; i < split.length;i++){

            boolean b = leadsService.deleteLeadsById(Integer.parseInt(split[i]));
            if(!b){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
    *修改
     */
    @RequestMapping("/toUpdateLeads")
    public String toUpdateLeads(Integer id,Model model){
        Leads leads = leadsService.selectLeadsById(id);
        List<User> users = userService.selectAllUser();
        model.addAttribute("users",users);
        model.addAttribute("leads",leads);
        return "leads/update_leads";
    }
    @RequestMapping("/updateLeads")
    @ResponseBody
    public Map updateLeas(Leads leads){
        System.out.println(leads);
        boolean b = leadsService.updateLeadsById(leads);
        System.out.println(b);
        Map map = new HashMap<>();
        if(b){
            map.put("success",true);
        }else{
            map.put("success",false);
        }
        return  map;
    }

    /**
     * 发邮件
     */
    @RequestMapping("/toSendEmail")
    public String toSendEmail(Integer id,HttpSession session,Model model){
        Leads leads = leadsService.selectLeadsById(id);
        String receiver = leads.getEmail();
        User user = (User)session.getAttribute("user");
        String sender = user.getEmail();
        model.addAttribute("zipcode",receiver);
        model.addAttribute("email",sender);
        return "marketing/sendemail_marketing";
    }


    /**
     * 领取线索
     */
    @RequestMapping("/drawLeads")
    @ResponseBody
    public Map drawLeads(Integer id,HttpSession session){
        Map map = new HashMap();
        User user = (User) session.getAttribute("user");
        boolean b = leadsService.updateOwnerUserId(id, user.getId());
       if(b){
           map.put("success",true);
           map.put("message","领取成功");
       }else{
           map.put("success",false);
           map.put("message","领取失败");
       }
        return map;
    }
    /**
     * 分配线索
     */
    @RequestMapping("/panDuan")
    @ResponseBody
    public Map panDuan(HttpSession session){
        User user = (User)session.getAttribute("user");
        Map map = new HashMap();
        if(user.getSortNum()>3){
            map.put("success",false);
            map.put("message","您没有权限分配");
        }else{
            map.put("success",true);
        }
        return map;
    }
    /**
     * 批量放入线索池
     */
    @RequestMapping("/putLeadsChi")
    @ResponseBody
    public Map putLeadsChi(String ids){
        String[] split = ids.split(",");

        boolean flag = true;
        for(int i = 0; i < split.length;i++){

            boolean b = leadsService.updateOwnerUserId(Integer.parseInt(split[i]),null);
            if(!b){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","放入线索池成功");
        }else{
            map.put("success",false);
            map.put("message","放入线索吃失败");
        }
        return map;
    }

    /**
     * 发送短信
     */
    @RequestMapping("/toSendMsg")
    public String toSendMsg(){
        return "marketing/sendmsg_marketing";
    }


}
